package com.sgb.knowledgeshare.view.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.ActivityShareLabeldetailsBinding;
import com.sgb.knowledgeshare.entity.TabelListEntity;
import com.sgb.knowledgeshare.view.adapter.LabelDetailsAdapter;
import com.sgb.knowledgeshare.view.adapter.LabelDetailsListAdapter;
import com.sgb.knowledgeshare.viewmodel.ShareLabelDetailsModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.model.entity.VideoBean;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SoftKeyBoardUtil;
import com.swgk.core.view.entity.VideoListEntity;
import java.io.Serializable;
import java.util.List;

/**
 * Created by：冯齐 on 2021/1/29 10:13
 * Description： 标签详情界面
 */
public class LabelDetailsActivity extends AppActivity {

    private ActivityShareLabeldetailsBinding binding;
    ShareLabelDetailsModel model;
    private LabelDetailsAdapter labeldetailsadapter;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private int pages;
    private List<VideoBean> listvideo;
    private boolean isLoadMore;
    private int page  = 1;
    private String labelNo;
    //标签检索页view
    private LabelDetailsListAdapter labelDetailsListAdapter;

    public static void start(Context context, String labelName,String labelNo) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("labelName", labelName);
        intent.putExtra("labelNo", labelNo);
        intent.setClass(context, LabelDetailsActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_share_labeldetails);
        model = new ViewModelProvider(this).get(ShareLabelDetailsModel.class);
        model.setActivity(LabelDetailsActivity.this);
        View decor = getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding.labelSearchEdtext.setText(getIntent().getStringExtra("labelName"));
        binding.labelSearchEdtext.setSelection(getIntent().getStringExtra("labelName").length());
        binding.labelSearchEdtext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                labelNo = "";
                //当用户输入完信息时检索标签信息
                model.getLabelList(s.toString().trim());
            }
        });
        labeldetailsadapter = new LabelDetailsAdapter(getActivity(), null);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(labeldetailsadapter);
        binding.labelLrv.setAdapter(mLRecyclerViewAdapter);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager( 2, StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //防止item位置互换
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        binding.labelLrv.setLayoutManager(layoutManager);
        com.github.jdsjlzx.ItemDecoration.GridItemDecoration divider = new GridItemDecoration.Builder(this)
                .setHorizontal(R.dimen.dp1)
                .setVertical(R.dimen.dp1)
                .setColorResource(R.color.white)
                .build();
        binding.labelLrv.addItemDecoration(divider);
        binding.labelLrv.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                labeldetailsadapter.getDatas().clear();
                page = 1;
                initData(page,labelNo);
                mLRecyclerViewAdapter.notifyDataSetChanged();
            }
        });
        binding.labelLrv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page++;
                if (page<=pages){
                    initData(page,labelNo);
                }else {
                    binding.labelLrv.setNoMore(true);
                }
            }
        });
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //跳转到播放列表数据
                Intent intent = new Intent(getActivity(), PlayListActivity.class );
                intent.putExtra(PlayListActivity.VIDEOTYPE,"fX_label");
                intent.putExtra(PlayListActivity.VIDEOTITLE,labelNo);
                intent.putExtra(PlayListActivity.VIDEOLISTBEAN, (Serializable) listvideo);
                intent.putExtra(PlayListActivity.PSO,position);
                intent.putExtra(PlayListActivity.PAGE,page);
                getActivity().startActivity(intent);
            }
        });
        binding.labelSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SoftKeyBoardUtil.hideKeyboard(getApplicationContext());
                binding.labelSearchEdtext.clearFocus();
                binding.rcview.setVisibility(View.GONE);
                if (TextUtils.isEmpty(labelNo)){
                    MToast.showToast(getApplicationContext(),"请选择标签信息！");
                    return;
                }
                if (!TextUtils.isEmpty(labelNo)){
                    page = 1;
                    initData(page,labelNo);
                }
            }
        });
        binding.backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.rcview.setVisibility(View.GONE);
        //标签页签列表
        labelDetailsListAdapter = new LabelDetailsListAdapter(this, null);
        binding.rcview.setLayoutManager(new LinearLayoutManager(this));
        binding.rcview.setAdapter(labelDetailsListAdapter);
        labelDetailsListAdapter.setOnItemClick(new AdapterOnItemClick<TabelListEntity>() {
            @Override
            public void onItemClick(TabelListEntity tabelListEntity, int position) {
                binding.labelSearchEdtext.setText(tabelListEntity.getLabelName());
                binding.labelSearchEdtext.setSelection(tabelListEntity.getLabelName().length());
                labelNo = tabelListEntity.getLabelNo();
            }
        });
    }

    //获取视频列表信息
    @Override
    protected void init() {
        labelNo = getIntent().getStringExtra("labelNo");
        initData(page,labelNo);
    }

    //初始化调用视频列表接口信息
    private void initData(int page,String labelNo) {
        isLoadMore = false;
        model.getVideLabelPage(page,labelNo);
    }
    //视频列表信息
    public void setVideLabelList(VideoListEntity data) {
        pages = data.getPages();
        List<VideoBean> records = data.getRecords();
        if (null == records) {
            return;
        }
        binding.labelLrv.refreshComplete(records.size());
        if (isLoadMore){
            if (records.size()>0){
                labeldetailsadapter.addDatas(records);
            }else {
                binding.labelLrv.setNoMore(true);
                MToast.showToast(this,"没有视频啦");
            }
        }else {
            if (records.size()>0){
                binding.labelLrv.setVisibility(View.VISIBLE);
                binding.rlNodeta.setVisibility(View.GONE);
                labeldetailsadapter.setDatas(records);
            }else {
                binding.labelLrv.setVisibility(View.GONE);
                binding.rlNodeta.setVisibility(View.VISIBLE);
                labeldetailsadapter.setDatas(null);
            }

        }
        this.listvideo = labeldetailsadapter.getDatas();
    }
    //标签列表
    public void setLabelList(List<TabelListEntity> data) {
        if (data==null&&data.size()==0)return;
        binding.rcview.setVisibility(View.VISIBLE);
        labelDetailsListAdapter.setDatas(data);
    }
}
